home *** CD-ROM | disk | FTP | other *** search
/ Windows Expert / Windows Expert.iso / utility / freemm.zip / FREEEMM.C < prev    next >
Text File  |  1987-12-02  |  4KB  |  167 lines

  1. /*  FreeEMM.c
  2.  *  Windows Application that displays free expanded memory
  3.  *  Requires MS C5.0 (for _dos_open ), will run with Windows 1.x and 2.x
  4.  *    Note: Compiled and tested with C5.0 and Windows 2.01
  5.  *
  6.  *  PUBLIC DOMAIN
  7.  *    MAY BE DISTRIBUTED IN AN UNMODIFED FORM WITHOUT OBLIGATION TO AUTHOR
  8.  *
  9.  *  Written By: David A. Hoatson, Spectrum Productions 1987
  10.  *  Spinoff of idea by: Charles Petzold
  11.  */
  12.  
  13. #define NOMINMAX        /* for screwed up windows.h file */
  14. #include <windows.h>
  15. #include <stdlib.h>
  16. #include <string.h>
  17. #include <fcntl.h>
  18. #include <dos.h>
  19. #include <stdio.h>
  20.  
  21. #define EMMINT 0x67
  22. #define EMMGETPAGES 0x42
  23. #define EMMGETVERSION 0x46
  24.  
  25. char verbuf[6];
  26.  
  27. emm_get_version( version )
  28.   unsigned char *version;
  29.   {
  30.   union REGS regs;
  31.   
  32.   regs.h.ah = EMMGETVERSION;
  33.   int86( EMMINT, ®s, ®s );
  34.   *version = regs.h.al;
  35.   
  36.   return( TRUE );
  37.   }
  38.  
  39. emm_get_num_pages( num_pages, unalloc_pages )
  40.   int *num_pages, *unalloc_pages;
  41.   {
  42.   union REGS regs;
  43.   
  44.   regs.h.ah = EMMGETPAGES;
  45.   int86( EMMINT, ®s, ®s );
  46.   *num_pages = regs.x.dx;
  47.   *unalloc_pages = regs.x.bx;
  48.   
  49.   return( TRUE );
  50.   }
  51.  
  52. /* Procedures which make up the window class. */
  53. long FAR PASCAL WndProc( hWnd, message, wParam, lParam )
  54.   HWND hWnd;
  55.   unsigned message;
  56.   WORD wParam;
  57.   LONG lParam;
  58.   {
  59.   static int mem, lastmem, maxmem;
  60.   char buffer[20];
  61.   PAINTSTRUCT ps;
  62.   RECT rect;
  63.   int num_pages, unalloc_pages;
  64.  
  65.   switch (message) {
  66.     case WM_TIMER:
  67.       emm_get_num_pages( &num_pages, &unalloc_pages );
  68.       maxmem = num_pages * 16;
  69.       mem = unalloc_pages * 16;
  70.       if (mem != lastmem)
  71.         InvalidateRect( hWnd, NULL, TRUE );
  72.       lastmem = mem;
  73.       break;
  74.  
  75.     case WM_PAINT:
  76.       BeginPaint( hWnd, (LPPAINTSTRUCT)&ps );
  77.  
  78.       /* Maximum Expanded memory in system */
  79.       strcat( itoa( maxmem, buffer, 10 ), "K" );
  80.  
  81.       /* Decided to hard code X and Y coords to fit in icon */
  82.       TextOut( ps.hdc, 0, 0, buffer, strlen( buffer ) );
  83.  
  84.       /* Available Expanded memory in system */
  85.       strcat( itoa( mem, buffer, 10 ), "K" );
  86.       TextOut( ps.hdc, 0, 12, buffer, strlen( buffer ) );
  87.       
  88.       /* Version number of EMM driver */
  89.       TextOut( ps.hdc, 0, 24, verbuf, strlen( verbuf ) );
  90.       EndPaint( hWnd, (LPPAINTSTRUCT)&ps );
  91.       break;
  92.  
  93.     case WM_QUERYOPEN:
  94.       break;
  95.       
  96.     case WM_DESTROY:
  97.       KillTimer( hWnd, 1 );
  98.       PostQuitMessage( 0 );
  99.       break;
  100.  
  101.     default:
  102.       return DefWindowProc( hWnd, message, wParam, lParam );
  103.       break;
  104.     }
  105.     return(0L);
  106. }
  107.  
  108. int PASCAL WinMain( hInstance, hPrevInstance, lpszCmdLine, cmdShow )
  109.   HANDLE hInstance, hPrevInstance;
  110.   LPSTR lpszCmdLine;
  111.   int cmdShow;
  112.   {
  113.   static char szAppName[] = "FreeEMM";
  114.   WNDCLASS WndClass;
  115.   MSG   msg;
  116.   HWND  hWnd;
  117.   int   fh;
  118.   struct EMMVERSION {
  119.     unsigned char low : 4;
  120.     unsigned char high: 4;
  121.     } emmversion;
  122.  
  123.   if (hPrevInstance)
  124.     return FALSE;
  125.  
  126.   /* Register Class for Main Windows */
  127.   WndClass.hCursor        = LoadCursor( NULL, IDC_ARROW );
  128.   WndClass.hIcon        = NULL;
  129.   WndClass.cbClsExtra        = 0;
  130.   WndClass.cbWndExtra        = 0;
  131.   WndClass.lpszMenuName        = szAppName;
  132.   WndClass.lpszClassName    = szAppName;
  133.   WndClass.hbrBackground    = (HBRUSH)GetStockObject( WHITE_BRUSH );
  134.   WndClass.hInstance        = hInstance;
  135.   WndClass.style        = CS_HREDRAW | CS_VREDRAW;
  136.   WndClass.lpfnWndProc        = WndProc;
  137.  
  138.   if (!RegisterClass( (LPWNDCLASS)&WndClass ) )
  139.     return(FALSE);
  140.  
  141.   if ( _dos_open( "EMMXXXX0", O_RDONLY, &fh ) != 0 ) {
  142.     MessageBox( hWnd, szAppName, "Expanded Memory Manager Not Found",
  143.         MB_OK | MB_ICONEXCLAMATION );
  144.     exit( 1 );
  145.     }
  146.  
  147.   emm_get_version( &emmversion );
  148.   sprintf( verbuf, "V%d.%d", emmversion.high, emmversion.low );
  149.   
  150.   hWnd = CreateWindow(szAppName, szAppName, WS_TILEDWINDOW,
  151.               0, 0, 0, 0, NULL, NULL, hInstance, NULL );
  152.  
  153.   if (!SetTimer( hWnd, 1, 1000, NULL ))
  154.     return FALSE;
  155.  
  156.   ShowWindow( hWnd, SW_SHOWMINIMIZED );
  157.   UpdateWindow( hWnd );
  158.  
  159.   /* Polling messages from event queue */
  160.   while (GetMessage( &msg, NULL, 0, 0 )) {
  161.     TranslateMessage( &msg );
  162.     DispatchMessage( &msg );
  163.     }
  164.  
  165.     return (int)msg.wParam;
  166.   }
  167.